package com.flyme.module.base.provider.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.module.base.client.api.SysDictRemoteService;
import com.flyme.module.base.client.entity.SysDict;
import com.flyme.module.base.client.entity.SysDictData;
import com.flyme.module.base.client.entity.Sysenum;
import com.flyme.module.base.provider.service.SysDictDataService;
import com.flyme.module.base.provider.service.SysDictService;
import com.flyme.module.base.provider.service.SysenumService;
import com.flyme.module.file.client.entity.SysCataLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author flyme
 */
@Slf4j
@Api(tags = "字典管理")
@RestController
@RequestMapping("/dict")
public class SysDictController extends BaseController<SysDictService, SysDict> implements SysDictRemoteService {


    @Autowired
    private SysDictDataService dictDataService;
    @Autowired
    private SysDictService dictService;
    @Autowired
    private SysenumService sysenumService;


    @ApiOperation(value = "获取全部数据")
    @PostMapping(value = "/selectAll")
    @Override
    public ResultBody selectAll() {
        QueryWrapper qw = new QueryWrapper<Map<String, Object>>();
        qw.select("id value", "dicTitle label","dicType");
        List list = bizService.listMaps(qw);
        return ResultBody.success(toCascader(list));
    }

    @ApiOperation(value = "添加字典")
    @PostMapping(value = "/add")
    @Override
    public ResultBody<Object> add(@ModelAttribute SysDict dict) {
        if (bizService.findByType(dict.getDicType()) != null) {
            return ResultBody.failed("字典类型Type已存在");
        }
        bizService.save(dict);
        dictService.initDictRedis();
        return ResultBody.ok("添加成功");
    }

    @ApiOperation(value = "编辑字典")
    @PostMapping(value = "/edit")
    @Override
    public ResultBody<Object> edit(@ModelAttribute SysDict dict) {
        SysDict old = bizService.getById(dict.getId());
        // 若type修改判断唯一
        if (!old.getDicType().equals(dict.getDicType()) && bizService.findByType(dict.getDicType()) != null) {
            return ResultBody.failed("字典类型Type已存在");
        }
        bizService.saveOrUpdate(dict);
        dictService.initDictRedis();
        return ResultBody.ok("编辑成功");
    }

    @ApiOperation(value = "通过id删除")
    @DeleteMapping(value = "/delByIds/{id}")
    @Override
    public ResultBody<Object> delAllByIds(@PathVariable Long id) {
        bizService.removeById(id);
        dictDataService.deleteByDictId(id);
        dictService.initDictRedis();
        return ResultBody.ok("删除成功");
    }

    @ApiOperation(value = "搜索字典")
    @GetMapping(value = "/search")
    @Override
    public ResultBody<List<SysDict>> search(@RequestParam String key) {
        List<SysDict> list = bizService.search(key);
        return ResultBody.success().setData(list);
    }

    @PostMapping(value="/getEnumData")
    public ResultBody<Object> getEnumData(){
        List<Sysenum> list=sysenumService.list();
        Map<String ,Object> maps=new HashMap<>();
        for (Sysenum sysenum : list) {
            maps.put(sysenum.getEnumCode(),sysenum.getScore());
        }
        return ResultBody.success().setData(maps);
    }
    /**
     * 更新分值
     *
     * @return
     */
    @PostMapping(value = "/updateEnums")
    public ResultBody updateEnums(@RequestParam("enumCode") String enumCode, @RequestParam("score") String score) {
        boolean result = sysenumService.update(new UpdateWrapper<Sysenum>().set("score", score).eq("enumCode", enumCode));
        return ResultBody.ok("更新成功").setData(result);
    }
}
